package com.lancoo.dictionary.data.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.os.Build;
import android.support.v7.widget.RecyclerView.Adapter;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lancoo.dictionary.R;
import com.lancoo.dictionary.data.model.Meaning;
import com.lancoo.dictionary.data.model.SampleSentence;
import com.lancoo.dictionary.utils.XBaseViewHolder;

import java.util.ArrayList;
import java.util.List;

public class SentenceAdapter extends Adapter<SentenceAdapter.SentenceViewHolder> {

	private static final String TAG = "SentenceAdapter";
	private List<SampleSentence> sampleSentences;
	private SearchResultAdapter searchResultAdapter;
	
	static class SentenceViewHolder extends XBaseViewHolder {

		private TextView itemSentence;
		private ImageView itemPlaySentence;
		
		public SentenceViewHolder(View itemView) {
			super(itemView);
			itemSentence = (TextView) itemView.findViewById(R.id.item_sentence);
			itemPlaySentence = (ImageView) itemView.findViewById(R.id.item_play_sentence);
		}
	}
	
	public SentenceAdapter(List<Meaning> meanings,SearchResultAdapter searchResultAdapter) {
		this.searchResultAdapter = searchResultAdapter;
		sampleSentences = new ArrayList<>();
		for (Meaning meaning:meanings) {
			if (meaning!=null) {
				List<SampleSentence> tmp = meaning.getSenCollection();
				if (tmp!=null) {
					sampleSentences.addAll(tmp);
				}
			}
		}
	}

	@Override
	public int getItemCount() {
		return sampleSentences.size();
	}

	@Override
	public void onBindViewHolder(SentenceViewHolder arg0, int arg1) {
		SampleSentence sampleSentence = sampleSentences.get(arg1);
		StringBuilder sb = new StringBuilder();
		sb.append(sampleSentence.getSentenceEn()).append("<BR/>").append(sampleSentence.getsTranslation());
		arg0.itemSentence.setText(Html.fromHtml(sb.toString()));


		String soundUri = sampleSentence.getsViocePath();
		if (TextUtils.isEmpty(soundUri)|| !soundUri.contains("mp3")) {
//			arg0.itemPlaySentence.setVisibility(View.INVISIBLE);
			arg0.itemPlaySentence.setVisibility(View.INVISIBLE);
		}else{
			arg0.itemPlaySentence.setVisibility(View.VISIBLE);
			searchResultAdapter.bindPlaySoundsListener(arg0.itemPlaySentence, sampleSentence.getsViocePath(),3+arg1);
			arg0.itemPlaySentence.setImageResource(R.drawable.lcdic_anim_blue_list_listen_volum_play);
			if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.KITKAT){
				if(searchResultAdapter.getSentenceIndex()==3+arg1){
					arg0.itemPlaySentence.setBackground(null);
					((AnimationDrawable)arg0.itemPlaySentence.getDrawable()).start();
				}else{
					arg0.itemPlaySentence.setBackgroundResource(R.drawable.lcdic_anim_blue_list_listen_volum_play);
					((AnimationDrawable)arg0.itemPlaySentence.getDrawable()).stop();
				}
			}else{
				if(searchResultAdapter.getSentenceIndex()==3+arg1){
					((AnimationDrawable)arg0.itemPlaySentence.getDrawable()).start();
				}else{
					((AnimationDrawable)arg0.itemPlaySentence.getDrawable()).stop();
				}
			}
		}
	}

	@Override
	public SentenceViewHolder onCreateViewHolder(ViewGroup viewgroup, int i) {
		return new SentenceViewHolder(LayoutInflater.from(viewgroup.getContext()).inflate(R.layout.lcdic_item_sentence, null));
	}
}
